package com.example.yyy

import android.content.Context
import android.os.Bundle
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat
import androidx.recyclerview.widget.*
import com.example.yyy.ItemClickSupport.OnItemListenerNew
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.activity_recycler_new.*
import kotlinx.android.synthetic.main.list_layout.*
import org.jetbrains.anko.toast


class RecyclerNewActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_new)
        var mutableList :MutableList<Planet> = mutableListOf()
        var planet = Planet(R.drawable.ic_launcher_background, "水星")
        mutableList.add(Planet(R.drawable.tp, "水星"))
        for (i in 1..20){
            mutableList.add(planet)
        }
        ry_ren_recycler1.layoutManager = GridLayoutManager(this,3)
        val adapter = RecyclerGridAdapter(this, mutableList)
        ry_ren_recycler1.adapter = adapter
        ry_ren_recycler1.itemAnimator = DefaultItemAnimator()
        val itemClickSupport = ItemClickSupport(ry_ren_recycler1)
        itemClickSupport.setItemListenerNew(object : OnItemListenerNew {
            override fun onItemClick(
                recyclerView: RecyclerView,
                itemView: View,
                position: Int
            ) {
                toast("你点击了${mutableList[position]}}")
            }
        })
        itemClickSupport.setItemLongListenerNew(adapter)
        ry_ren_recycler2.layoutManager = StaggeredGridLayoutManager(3, DividerItemDecoration.VERTICAL)
        val adapter1 = RecyclerStateAdapter(this, mutableList)
        ry_ren_recycler2.adapter = adapter1
        ry_ren_recycler2.itemAnimator = DefaultItemAnimator()
        ry_ren_recycler2.addOnItemTouchListener(SimpleRecycleViewItemClickListener(adapter1))

    }
    class RecyclerGridAdapter(context: Context,private val infos:MutableList<Planet>):RecyclerBaseAdapter<RecyclerView.ViewHolder>(context),
        ItemClickSupport.OnItemLongListenerNew{

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view:View? = inflater.inflate(R.layout.list_layout,parent,false)
            return ItemHolder(view)
        }

        internal class ItemHolder(itemView: View?): RecyclerView.ViewHolder(itemView!!) {
            var im: ImageView = itemView!!.findViewById(R.id.iv_lt_planet)
            var tv: TextView = itemView!!.findViewById(R.id.tv_lt_planet)
        }

        override fun getItemCount(): Int = infos.size

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val planet = infos[position]
            var vh: ItemHolder = holder as ItemHolder
            vh.im.setImageResource(planet.image)
            vh.tv.text = planet.str
        }


        override fun onItemLongClick(view: RecyclerView, itemView: View, position: Int): Boolean {
            context.toast("你长按了${infos[position]}")
            return true
        }

    }

    //RecyclerView已有的方法addOnItemTouchListener()实现交互事件
     class SimpleRecycleViewItemClickListener(private val mListener:OnItemClickListener): RecyclerView.OnItemTouchListener{
        var mGestureDetector: GestureDetectorCompat?=null
        override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {

        }

        override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
            if (mGestureDetector==null) initGestureDetector(rv)

            return  mGestureDetector!!.onTouchEvent(e)
        }

        override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {

        }

        private fun initGestureDetector(recyclerView: RecyclerView) {
            mGestureDetector = GestureDetectorCompat(recyclerView.context, object : GestureDetector.SimpleOnGestureListener() {
                /**
                 * 单击事件
                 */
                override fun onSingleTapUp(e: MotionEvent): Boolean {
                    val childView = recyclerView.findChildViewUnder(e.x, e.y)
                    if (childView != null) {
                        mListener.onItemClick(
                            childView,
                            recyclerView.getChildLayoutPosition(childView)
                        )
                    }
                    return true
                }

                /**
                 * 双击事件
                 */
                override fun onDoubleTap(e: MotionEvent): Boolean {
                    val childView = recyclerView.findChildViewUnder(e.x, e.y)
                    if (childView != null) {
                        mListener.onItemDoubleClick(
                            childView,
                            recyclerView.getChildLayoutPosition(childView)
                        )
                    }
                    return true
                }

                /**
                 * 长按事件
                 */
                override fun onLongPress(e: MotionEvent) {
                    val childView = recyclerView.findChildViewUnder(e.x, e.y)
                    if (childView != null) {
                        mListener.onItemLongClick(
                            childView,
                            recyclerView.getChildLayoutPosition(childView)
                        )
                    }
                }
            })
        }

        /**
         *
         */
        interface OnItemClickListener {
            /**
             * 当ItemView的单击事件触发时调用
             */
            fun onItemClick(view: View?, position: Int)

            /**
             * 当ItemView的长按事件触发时调用
             */
            fun onItemLongClick(view: View?, position: Int)

            /**
             * 当ItemView的双击事件触发时调用
             */
            fun onItemDoubleClick(view: View?, position: Int)
        }
    }
    }
        //anko
    class  RecyclerStateAdapter(context: Context,private val infos:MutableList<Planet>):RecyclerBaseAdapter<RecyclerView.ViewHolder>(context),
        RecyclerNewActivity.SimpleRecycleViewItemClickListener.OnItemClickListener{
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view:View? = inflater.inflate(R.layout.list_layout,parent,false)
            return ItemHolder(view)
        }

        class ItemHolder(override val containerView: View?): RecyclerView.ViewHolder(containerView!!),LayoutContainer {
            fun bind(planet:  Planet){
                iv_lt_planet.setImageResource(planet.image)
                tv_lt_planet.text = planet.str
            }
        }

        override fun getItemCount(): Int = infos.size

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder as ItemHolder).bind(infos[position])
        }

            override fun onItemClick(view: View?, position: Int) {
                context.toast("你单击按了${infos[position]}")
            }

            override fun onItemLongClick(view: View?, position: Int) {
                context.toast("你长按了${infos[position]}")
            }

            override fun onItemDoubleClick(view: View?, position: Int) {
                context.toast("你双击按了${infos[position]}")
            }

}